<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入项目名称" :clearable="true" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
        <el-button @click="handleClickCreate"> 新建 </el-button>
      </el-form-item>
    </el-form>
    <Table
      :columns="columns"
      :page-param="queryParams"
      :adaptive="true"
      :page-data="queryData"
      save-key="Project"
      height-per="90"
      @page-change="getPage"
      @row-dblclick="handleTableDblclick"
    >
      <template #menu="{ row }">
        <context-menu-item label="修改" @click="handleClickUpdate(row)" v-hasPermi="['']" />
        <context-menu-item label="删除" @click="handleClickDelete(row)" v-hasPermi="['']" />
      </template>
    </Table>
  </ContentWrap>
  <ProjectEdit ref="projectEditRef" @success="getPage" />
</template>
<script setup name="Project" lang="ts">
import ProjectEdit from './ProjectEdit.vue'
import * as DevopsProjectApi from '@/api/infra/devops/project'
const projectEditRef = ref()
const queryFormRef = ref()
const queryParams = ref({
  name: undefined
})
const queryData = ref()
const columns = [
  {
    label: '项目名称',
    prop: 'name'
  },
  {
    label: '项目编号',
    prop: 'projectNo'
  }
]
/** 初始化 */
onMounted(() => {
  getPage()
})

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 新建按钮操作 */
const handleClickCreate = () => {
  projectEditRef.value.open('create', 0)
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await DevopsProjectApi.pageQuery(queryParams.value)
}

/** 处理table行双击事件 */
const handleTableDblclick = (row) => {
  projectEditRef.value.open('update', row.id)
}

/** 处理右击事件 修改 */
const handleClickUpdate = (row) => {
  projectEditRef.value.open('update', row.id)
}

/** 处理右击事件 删除 */
const handleClickDelete = async (row) => {
  await DevopsProjectApi.deleted(row.id)
  getPage()
}
</script>
